<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Request;
class Erp extends Controller
{
    public function index(){
    	if(Request::instance()->isPost()){
    		$data_json=reset($_POST);//获取第一个传参
    		//判断json函数
    		function is_json($string) {
				 json_decode($string);
				 return (json_last_error() == JSON_ERROR_NONE);
			}

			$check_json=is_json($data_json);
			
			if($check_json){
				$data=json_decode($data_json,ture);
				// type:0查询 1增加 2扣除    tel:手机号   integral:积分变动数量  order: 订单号
				if(!$data['tel']){return return_code(10030);}
				if( $data['type'] !=0 && $data['type'] !=1 && $data['type'] !=2){return return_code(10040);}
				if(!$data['type'] && $data['type'] !=0){return return_code(10050);}
				if($data['type'] ==''){return return_code(10050);}
				
				//实例化
				$user_integral=Db::name('user_integral');
				$erp_order=Db::name('erp_order');

				if($data['type']==0){
					//ERP 发送查询的指令，先判断我们有没有，没有则调出ERP的数据
					//BUG（对方请求增加或者扣费，这边没有积分变动）
					$integral=$user_integral->where(array('tel' =>$data['tel']))->find();

					if(!$integral){
						//不存在则查询积分然后写数据库
						$user_erp_integral=erp_active($data['tel']);
						$user_erp_integral=json_decode($user_erp_integral,ture);
						$creat['tel']=$user_erp_integral['tel'];
						$creat['integral']=$user_erp_integral['integral'];
						$user_integral-> insert($creat);
					}
					//存在则返回
					$info_json['code']='10000';
					$info_json['msg']='调用成功';
					$info_json['tel']=$integral['tel'];
					$info_json['integral']=$integral['integral'];
					
					return json_encode($info_json);
				}else if($data['type']==1 || $data['type']==2){

					if(!$data['integral'] && $data['integral'] != 0){return return_code(10060);}
					$rest_num=is_numeric($data['integral']);
					if(!$rest_num || $data['integral'] < 0){return return_code(10070);}

					//订单号判断
					if(!$data['order']){return return_code(10080);}
					$rest = substr($data['order'],0,5);//判断是否“smerp”开始
					$rest_strlen=strlen($data['order']);//判断长度是否正确
					if($rest!='cmerp' || $rest_strlen !=23){return return_code(10090);}

					//判断是否存在订单
					$check_order=$erp_order->where(array('order' =>$data['order']))->find();
					if($check_order){return return_code(10100);}

					//创建订单
					$creat_order['order']=$data['order'];
					$creat_order['tel']=$data['tel'];
					$creat_order['type']=$data['type'];
					$creat_order['integral']=$data['integral'];
					$erp_order-> insert($creat_order);

					//增加积分后，重新获取ERP的积分
					$integral=$user_integral->where(array('tel' =>$data['tel']))->find();
					$user_erp_integral=erp_active($data['tel']);
					$user_erp_integral=json_decode($user_erp_integral,ture);
					if(!$integral){
						$creat['tel']=$user_erp_integral['tel'];
						$creat['integral']=$user_erp_integral['integral'];
						$user_integral-> insert($creat);
					}else{
						$upwhere['tel']=$user_erp_integral['tel'];
						$update['integral']=$user_erp_integral['integral'];
						$user_integral->where($upwhere)->update($update);
					}
					//END
					

					$user=Db::name('user')->where('telphone',$data['tel'])->find();
					if($user){
						$user['wxname']=base64_decode($user['username']);

		
				        if($data['type']==1){
				            $bd='增加'.$data['integral'].'积分';
				        }else if($data['type']==2){
				            $bd='减少'.$data['integral'].'积分';
				        }else{
				            $bd='';
				        }


						$user_erp_integral=erp_active($user['telphone']);
						$user_erp_integral=json_decode($user_erp_integral,true);


				        $postdata = array(
				            'first' => array(
				                'value' => '尊敬的会员您积分变动如下:',
				                'color' => '#FF0000'
				            ),
				            'keyword1' => array(
				                'value' => $bd,
				                'color' => '#000'
				            ),
				            'keyword2' => array(
				                'value' => '已完成',
				                'color' => '#000'
				            ),
				            'keyword3' => array(
				                'value' => $user_erp_integral['integral'],
				                'color' => '#000'
				            ),
				            'remark' => array(
				                'value' => '感谢您再次光临！',
				                'color' => '#000'
				            )
				        );
				        $wxurl='http://www.sxjhol.vip/index.php/index/user';
				        send_wx($postdata,$user['openid'],'61KQO8sAOYJLqZM8bwGsIFxnmE608MEv0ueie9iG9yA',$wxurl);
					}
					return return_code(10000);
				}


			}else{
				return return_code(10020);
			}
    	}else{
    		return return_code(10010);
    	}
    }


}
